<template>
  <div class="userm-page">
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div style="margin-top: 15px">
        <el-input
          placeholder="请输入内容"
          v-model="userInfo.query"
          clearable
          @clear="getUserListInfo"
        >
          <template slot="append"
            ><i class="el-icon-search" @click="getUserListInfo"></i
          ></template>
        </el-input>
        <el-button type="primary" @click="dialogVisible = true"
          >添加用户</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="{ row }">
            <el-switch v-model="row.mg_state" @change="changeState(row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="175px">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editUserInfoList(row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delUserInfoList(row.id)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="userInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="userInfo.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input autocomplete="off" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            autocomplete="off"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input autocomplete="off" v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input autocomplete="off" v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserInfoList">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="30%"
      @close="editUserInfo"
    >
      <el-form
        :model="editForm"
        status-icon
        :rules="EditRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="用户名">
          <el-input
            autocomplete="off"
            v-model="editForm.username"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input autocomplete="off" v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input autocomplete="off" v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserList(editForm.id)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import {
  getUserListApi,
  changeStateApi,
  addUserInfoListApi,
  delUserInfoListApi,
  editUserInfoListApi,
  editGetUserInfoListApi,
} from "@/api/user";
export default {
  name: "userm-page",
  data() {
    return {
      tableData: [],
      userInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
        total: 0,
      },
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            pattern: /^\w{5,11}$/,
            message: "请输入 5 到 10 位的密码",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/,
            message: "请输入正确的邮箱",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
      },
      dialogVisible: false,
      editDialogVisible: false,
      editForm: {
        username: "",
        email: "",
        mobile: "",
      },
      EditRules: {
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/,
            message: "请输入正确的邮箱",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getUserListInfo();
  },
  methods: {
    async getUserListInfo() {
      const res = await getUserListApi(this.userInfo);
      console.log(res);
      this.tableData = res.data.users;
      this.userInfo.total = res.data.total;
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.userInfo.pagesize = newSize;
      this.getUserListInfo();
    },
    handleCurrentChange(newCurrent) {
      console.log(newCurrent);
      this.userInfo.pagenum = newCurrent;
      this.getUserListInfo();
    },
    async changeState(info) {
      const res = await changeStateApi(info.id, info.mg_state);
      console.log(res);
      if (res.meta.status === 200) return Message.success("修改状态成功");
    },
    closeDialog() {
      this.$refs.ruleForm.resetFields();
    },
    async addUserInfoList() {
      this.dialogVisible = true;
      await this.$refs.ruleForm.validate();
      await addUserInfoListApi(this.ruleForm);
      Message.success("添加成功");
      this.userInfo.pagenum = 1;
      this.getUserListInfo();
      this.dialogVisible = false;
    },
    async delUserInfoList(id) {
      await this.$confirm("确认删除");
      await delUserInfoListApi(id);
      Message.success("删除成功");
      this.getUserListInfo();
    },
    async editUserInfoList(id) {
      this.editDialogVisible = true;
      const res = await editGetUserInfoListApi(id);
      this.editForm = res.data;
    },
    async editUserList(id) {
      await this.$refs.editForm.validate();
      await editUserInfoListApi(id, {
        email: this.editForm.email,
        mobile: this.editForm.mobile,
      });
      Message.success("修改成功");
      this.getUserListInfo();
      this.editDialogVisible = false;
    },
    editUserInfo() {
      this.$refs.editForm.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  text-align: left;
  margin-top: 50px;
  ::v-deep .el-input-group {
    width: 40%;
    margin: 0 50px 30px 20px;
  }
  .el-pagination {
    margin-top: 30px;
  }
}
.el-dialog {
  text-align: left;
}
</style>
